package com.astrob.adapters;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.astrob.model.HistoryData;
import com.astrob.model.RoutePlanDataHandle;
import com.astrob.nhcmap.R;

public class PlanDestAdapter extends BaseAdapter {
	public static final int MAX_DEST_COUNT = 8;
	static class ViewHolder{
		ImageButton action;
		TextView text;
		ImageButton soundInput;
	}
	Context mContext = null;
	public PlanDestAdapter(Context context){
		mContext = context;
	}
	@Override
	public int getCount() {
		int count = RoutePlanDataHandle.getInstance().getDests().size();
		if(count >= MAX_DEST_COUNT){
			return count+1;
		}
		return count+2;
	}
	private int getDestCount(){
		return RoutePlanDataHandle.getInstance().getDests().size();
	}
	@Override
	public Object getItem(int position) {
		if(position == 0){
			return RoutePlanDataHandle.getInstance().getStart();
		} else if(position <= getDestCount()){
			return RoutePlanDataHandle.getInstance().getDests().get(position-1);
		}
		return new HistoryData();
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	public interface DestItemListener{
		//当只有起点时，不能切换
		public void onSwitch();
		
		public void onAdd();
		public void onDel(int pos);
		
		public void onChangeStart();
		public void onChangeDest(int pos);
		public void onSoundInput();
	}
	DestItemListener mListener = null;
	public void setListener(DestItemListener listener){
		mListener = listener;
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (null == convertView) {
			convertView = LinearLayout.inflate(mContext,
					R.layout.adapter_routeplan_dest, null);

			holder = new ViewHolder();
			holder.action = (ImageButton)convertView.findViewById(R.id.route_dest_action);
			holder.text = (TextView) convertView.findViewById(R.id.route_dest_text);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		int destCount = getDestCount();
		HistoryData data = (HistoryData) getItem(position);
		boolean isAddItem = false;
		if(position == destCount+1){
			if(MAX_DEST_COUNT == destCount){
				isAddItem = false;
			} else{
				isAddItem = true;
			}
		}
		if(position == 0){
			//起点，比不为空
			Drawable drawable= mContext.getResources().getDrawable(R.drawable.point_start);
		    drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());  
		    holder.text.setCompoundDrawables(drawable,null,null,null);
		    holder.text.setTextColor(0xff3b90df);
			holder.text.setText(data.getName());
			if(destCount > 1){ //超过1个目的地不用交换目的地
				holder.action.setVisibility(View.INVISIBLE);
			}else{
				holder.action.setBackgroundResource(R.drawable.selector_btn_route_change);
				holder.action.setVisibility(View.VISIBLE);
				holder.action.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						if(mListener != null){
							mListener.onSwitch();
						}
					}
				});
			}
		} else {
			Drawable drawable= mContext.getResources().getDrawable(R.drawable.point_end);
		    drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());  
		    holder.text.setCompoundDrawables(drawable,null,null,null);
		    holder.text.setTextColor(0xff303030);
			holder.text.setText(data.getName());
			holder.action.setVisibility(View.VISIBLE);
			if (isAddItem) {
				// 待添加的目的地
				holder.action.setBackgroundResource(R.drawable.selector_btn_route_add);
				holder.action.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						if(mListener != null){
							mListener.onAdd();
						}
					}
				});
			} else {
				// 已添加的目的地
				final int pos = position-1;
				holder.action.setBackgroundResource(R.drawable.selector_btn_route_delete);
				holder.action.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						if(mListener != null){
							mListener.onDel(pos);
						}
					}
				});
			}
		}
		final boolean bAddMode = isAddItem; 
		final int pos = position-1;
		holder.text.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(mListener != null){
					if(bAddMode){
						mListener.onAdd();
					} else if(pos == -1){ //position == 0
						mListener.onChangeStart();
					} else {
						mListener.onChangeDest(pos);
					}
				}				
			}
		});
		return convertView;
	}

}
